<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
use think\Request;
use think\Validate;

class CarModel extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    protected function filter(&$map)
    {
        if ($this->request->param("car_model")) {
            $map['car_model'] = ["like", "%" . $this->request->param("car_model") . "%"];
        }
    }

    /**
     * 添加页面
     */
    public function add(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post(); // 接收数据
            // 验证数据
            $rule = [
                'car_brand_id'=>'require',
                'car_model' => 'require',
                'seat' => 'require'
            ];
            $message = [
                'car_brand_id.require' => '车辆品牌不能为空',
                'car_model.require' => '汽车型号不能为空',
                'seat.require' => '座位数不能为空',
            ];
            $validate = new Validate($rule,$message);
            $result = $validate->check($postData);
            if (!$result){
                return ajax_return_error($validate->getError());
            }
            //查询车型id
           $postData['arctic_id'] =  Db::name('car_brand')->where('id',$postData['car_brand_id'])->value('arctic_id');
            // 保存数据
            $postData['create_time'] = time();
            $addId = Db::name('car_model')->strict(false)->insertGetId($postData);
            if ($addId){
                return ajax_return_adv('添加成功');
            }else{
                return ajax_return_error('添加失败');
            }
        }else{
            // 查询车型
           $arctic =  Db::name('arctic')->field('id,name')->select();
           // 查询车牌
           $carBrand = Db::name('car_brand')->field('id,arctic_id,brand_name')->select();
           $list = [];
           if ($carBrand){
               foreach ($carBrand as $cbk=>$cbv){
                    if ($arctic){
                        foreach ($arctic as $ak=>$av){
                            if ($cbv['arctic_id'] == $av['id']) {
                                $carBrand[$cbk]['name'] = $av['name'];
                            }
                        }
                    }
                   if (empty($carBrand[$cbk]['name'])){
                       unset($carBrand[$cbk]);//销毁没有车型的品牌
                   }else{
                       $list[] = [
                           'brand_name' => "车辆类型:{$carBrand[$cbk]['name']}=>车辆品牌:{$carBrand[$cbk]['brand_name']}",
                           'id' => $carBrand[$cbk]['id'],
                       ];
                   }
               }
           }
           // 座位数
            $seats = [];
           for ($i=1;$i<81;$i++){
               $seats[] = [
                   'value'=>$i,
                   'name'=>"座位数为{$i}"
               ];
           }
          return view('edit',compact('list','seats'));
        }
    }

    /**
     * 编辑页面
     */
    public function edit(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();
            //验证数据
            $rule = [
                'id'=>'require',
                'car_brand_id'=>'require|number',
                'car_model' => 'require',
                'seat' => 'require'
            ];
            $message = [
                'id.require' => '缺少必要的参数',
                'car_brand_id.require' => '车辆品牌不能为空',
                'car_brand_id.number' => '车辆品牌格式不正确',
                'car_model.require' => '汽车型号不能为空',
                'seat.require' => '座位数不能为空',
            ];
            $validate = new Validate($rule,$message);
            $result = $validate->check($postData);
            if (!$result){
                return ajax_return_error($validate->getError());
            }
            // 更新数据
            $up = Db::name('car_model')->strict(false)->update($postData);
            if ($up){
                return ajax_return_adv('更新数据成功');
            }else{
                return ajax_return_error('更新数据失败或者数据不变动');
            }
        }else{
            // 查询数据
            $data = input();
            if (!$data['id']){
                return ajax_return_error('缺少参数');
            }
            $vo = Db::name('car_model')->where('id',$data['id'])->find();
            if (!$vo){
                return ajax_return_error('不存在该汽车型号信息');
            }
            // 查询车型
            $arctic =  Db::name('arctic')->field('id,name')->select();
            // 查询车牌
            $carBrand = Db::name('car_brand')->field('id,arctic_id,brand_name')->select();
            $list = [];
            if ($carBrand){
                foreach ($carBrand as $cbk=>$cbv){
                    if ($arctic){
                        foreach ($arctic as $ak=>$av){
                            if ($cbv['arctic_id'] == $av['id']) {
                                $carBrand[$cbk]['name'] = $av['name'];
                            }
                        }
                    }
                    if (empty($carBrand[$cbk]['name'])){
                        unset($carBrand[$cbk]);//销毁没有车型的品牌
                    }else{
                        $list[] = [
                            'brand_name' => "车辆类型:{$carBrand[$cbk]['name']}=>车辆品牌:{$carBrand[$cbk]['brand_name']}",
                            'id' => $carBrand[$cbk]['id'],
                        ];
                    }
                }
            }
            // 座位数
            $seats = [];
            for ($i=1;$i<81;$i++){
                $seats[] = [
                    'value'=>$i,
                    'name'=>"座位数为{$i}"
                ];
            }
            return view('edit',compact('list','seats','vo'));
        }
    }

    /**
     * 首页
     */
    public function index()
    {
        $data = input(); // 接收数据
        //搜索条件
        if ($data['arctic_id']){ // 车辆类型
            $map['arctic_id'] = $data['arctic_id'];
        }
        if ($data['car_brand_id']){ // 车牌
            $carBrandIds = Db::name('car_brand')->where('brand_name','like',"%{$data['car_brand_id']}%")->field('id')->select();
            $brandIds = '';
            if ($carBrandIds){
                foreach ($carBrandIds as $cbk=>$cbv){
                    $brandIds .= $cbv['id'].',';
                }
                $brandIds =  rtrim($brandIds,',');
            }
            if ($brandIds){
                $map['car_brand_id'] = ['in',$brandIds];
            }
        }
        if ($data['create_time']){
            $start_time = "{$data['create_time']} 0:0:0"; //开始时间
            $end_time = "{$data['create_time']} 23:59:59"; // 结束时间
            $map['create_time'] = ['between',[strtotime($start_time),strtotime($end_time)]];
        }
        // 汽车型号
        if ($data['car_model']){
            $map['car_model'] = ['like',"%{$data['car_model']}%"];
        }
        if ($data['seat']){
            $map['seat'] = $data['seat'];
        }
        if ($this::$isdelete){
            $map['isdelete'] = 1;
        }else{
            $map['isdelete'] = 0;
        }
        //查询数据
        $listRows = $this->request->param('numPerPage') ? 20: config("paginate.list_rows");
        $list = Db::name('car_model')->where($map)->order('id','desc')->paginate($listRows, false, ['query' => $this->request->get()]);
        $count = $list->total();
        $page = $list->render();
        // 查询所有车型
        $arctic = Db::name('arctic')->field('id,name')->order('id','asc')->select();
        $this->view->assign('list',$list);
        $this->view->assign('count',$count);
        $this->view->assign('arctic',$arctic);
        $this->view->assign('page',$page);
        return $this->view->fetch();
    }
}
